<?php
/**
 * @TODO Fill Header
 *
 * @author Xavier VILAIN
 * @package
 * @subpackage
 */
use Doctrine\DBAL\Connection;
use Fox\Generator;
use Fox\MaterialManagement\Material\Hierarchy;
use Fox\Html\DropList;
use Fox\Html\Screen;
use Fox\MaterialManagement\Material;
use Fox\Html\TextArea;

$xview = Xview::instance();
$id = Xview::instance()->val("inquiryid");
$prdh01 = Xview::instance()->val("prdh01");
$excluded = array();

// Selection Screen
$screen = new Screen("eSelectItem");
$block = new Fox\Html\Block("bSelectH");
$block->add(new TextArea("excluded"))->hidden();
// Load already selected Product
$blItems = Xview::instance()->component("inquiry", "items");
$data = $blItems->item("gItems")->get();
if(is_array($data))
{
	foreach($data as $line)
	{
		if(isset($line['materialid']))
			$productId[] = $line['materialid'];
	}
	if(isset($productId))
	{
		$excluded = new \Fox\Dao\SqlFragment("materialid NOT IN (?) ",array($productId),array(Connection::PARAM_STR_ARRAY));
		$json = addslashes(json_encode($productId));
		$block->item("excluded")->default = $json;
	}
}
else
{
	$values = Xview::instance()->val("excluded");

	if(!empty($values))
	{
		$productId = json_decode(stripcslashes($values),true);
		$excluded = new \Fox\Dao\SqlFragment("materialid NOT IN (?) ",array($productId),array(Connection::PARAM_STR_ARRAY));
	}
}

// Level 1 :
$finder = new Hierarchy();
$fragment = new Fox\Dao\SqlFragment("level = ?",array(1));
$branches = Hierarchy::loadTree(array($fragment));
// Select a branch First
$action = "ReloadFromObject('eSelectItem','pSelectMaterials','?mod=inquiry&act=materials&sac=add&inquiryid=$id')";
$block->add(new DropList("prdh01",true,false,true))
			->onChange($action)
			->options($branches,"code","label");
$screen->add($block);

if(!empty($prdh01))
{
	$blItm = new Fox\Html\Block("bSelectItems",Msg(Material::BUNLDE_NAME,23));
	$tree = new \Fox\Grid\Tree("hierarchyTreeSel");
	$tree->fieldcat(array("label","materialid"));
	$tree->setFieldAttr(array("materialid"), "SetHidden",true);
	$tree->setFieldAttr(array("materialid","label"), "SetHiddenInput",true);
	$tree->setCoche(true);
	$tree->setExpandLevel(99);

	$root = new Fox\Node(Fox\Node::ROOT_NODE);
	// Load all product according to hierarchy selected code
	$finder = new Material();
	//$filters[] = $excluded;
	$filters[] = new \Fox\Dao\SqlFragment("hierarchy = ? AND active = 1",array($prdh01));
	$materials = $finder->find(Fox\Dao\SqlFragment::combine($filters));
	//
	foreach($materials as $material)
	{
		/* @var $material Material */
		$data = $material->getData();
		$data['materialid'] = $material->getId();
		$data['componentid'] = $data['materialid'];
		$bomRoot = $material->bom(10);
		$bomRoot->setProperties($data);
		$root->add($bomRoot);

	}
	$tree->initialize($root,true);
	$tree->fieldcat()->behaviors(Fox\Behaviors::key(Fox\Generator::concatenate(Fox\Generator::useElement('materialid'), Fox\Generator::useElement('componentid'),":")));
	$tree->setExpandLevel(0);
	$blItm->add($tree);
	$tree->footer()->add(new Fox\Html\Button('select',"selectMaterials('hierarchyTreeSel');CloseWin('addMaterials',true);"))
				->closeOnClick(true);
	if($prdh01 == "06" || Fox\Authorization\Manager::check(Material::AUTH_CREATE))
	{
		$tree->footer()->add(new Fox\Html\Button('addnew',Fox\Generator::display("pAddMaterial",
										 Fox\Generator::url(
										 	"material",
										 	"create",
										 	"ontheflow",
										 	array("hierarchy" => Fox\Generator::useValue($prdh01))
										 	)
										),
										"Nouvelle"
								));
	}
	$screen->add($blItm);
}

if(isset($_POST['prdh01']))
{
	$inputs = $screen->get();
	if(!$screen->errors())
	{

	}
}

$popup = new Fox\Popup("pSelectMaterials",Msg(Material::BUNLDE_NAME,22),$screen,600);
$o_shlp = new Fox\Html\SearchHelp("Material_by_Hierarchy");
echo $popup->display();
